package iot.core.data;

import iot.core.data.mapper.MyBaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@SuppressWarnings("unchecked")


public abstract class ServiceImpl<M extends MyBaseMapper<T>, T> implements  IService<  T> {
    @Autowired
    protected M baseMapper;

    @Override
    public M getBaseMapper() {

        return baseMapper;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(T entity){
        return getBaseMapper().insert(entity,true);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(T entity){
        return getBaseMapper().updateById(entity,true);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(T entity){
        return getBaseMapper().deleteById(entity,true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(BaseQto entity){
        var m=getBaseMapper().selectById(entity.getId());
        return delete(m);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int DeleteById(List<T> l){
        var r=0;
       for(var m:l){
           r+= getBaseMapper().deleteById(m,true);
       }
       return r;
    }

}
